#!/usr/bin/env python3
# -*- encoding: utf-8 -*-


"""
rtsp实时网络摄像头捕获和云台控制
"""

import cv2 as cv

from tools.camera.cgi_get_api import ptz_control
from tools.camera.search_camera import getCameraIp

CMD_Control = {}
CMD_Control['CMD_PTZ_UP'] = 0  # 上
CMD_Control['CMD_PTZ_UP_STOP'] = 1  # 上停
CMD_Control['CMD_PTZ_DOWN'] = 2  # 下
CMD_Control['CMD_PTZ_DOWN_STOP'] = 3  # 下停
CMD_Control['CMD_PTZ_LEFT'] = 4  # 左
CMD_Control['CMD_PTZ_LEFT_STOP'] = 5  # 左停
CMD_Control['CMD_PTZ_RIGHT'] = 6  # 右
CMD_Control['CMD_PTZ_RIGHT_STO'] = 7  # 右停
CMD_Control['CMD_PTZ_CENTER'] = 25  # 自动巡航并居中

CMD_Control['CMD_PTZ_IRCUT_HIGH'] = 96  # 开Ir_Cut
CMD_Control['CMD_PTZ_IRCUT_LOW'] = 97  # 关Ir_Cut


class rtspStream():
    @staticmethod
    def getCamerIP():
        return getCameraIp()

    def __init__(self):
        self.cameraIP = self.getCamerIP()
        prot = '10554'
        play_adder = '/tcp/av0_1'
        rtsp_adder = ''.join(('rtsp://', 'admin:888888@', self.cameraIP, ':', prot, play_adder))
        self.cap = cv.VideoCapture(rtsp_adder)
        if not self.cap.isOpened():
            raise ("error")

    def keyScan(self, k):
        k = chr(k)
        if k == 'w':
            ptz_control(self.cameraIP, command = CMD_Control.get('CMD_PTZ_UP'))
        elif k == 's':
            ptz_control(self.cameraIP, command = CMD_Control.get('CMD_PTZ_UP_STOP'))
        elif k == 'x':
            ptz_control(self.cameraIP, command = CMD_Control.get('CMD_PTZ_DOWN'))
        elif k == 'a':
            ptz_control(self.cameraIP, command = CMD_Control.get('CMD_PTZ_LEFT'))
        elif k == 'd':
            ptz_control(self.cameraIP, command = CMD_Control.get('CMD_PTZ_RIGHT'))

        elif k == 'c':
            ptz_control(self.cameraIP, command = CMD_Control.get('CMD_PTZ_CENTER'))

        elif k == 'i':
            ptz_control(self.cameraIP, command = CMD_Control.get('CMD_PTZ_IRCUT_HIGH'))

        elif k == 'o':
            ptz_control(self.cameraIP, command = CMD_Control.get('CMD_PTZ_IRCUT_LOW'))

        elif k == "1":
            # 设置预设位1 向左
            ptz_control(app.cameraIP, command = 0x30)
        elif k == '2':
            # 设置预设位2  默认
            ptz_control(app.cameraIP, command = 0x32)
        elif k == '3':
            # 设置预设位3  向右
            ptz_control(app.cameraIP, command = 0x34)
        elif k == "4":
            # 调用预设位1 向左
            ptz_control(app.cameraIP, command = 0x31)
        elif k == '5':
            # 调用预设位2  默认
            ptz_control(app.cameraIP, command = 0x33)
        elif k == '6':
            # 调用预设位3  向右
            ptz_control(app.cameraIP, command = 0x35)

    def update_frame(self):
        while self.cap.isOpened():
            ret, self.frame = self.cap.read()
            if not ret:
                break

            k = cv.waitKey(1) & 0xff
            self.keyScan(k)
            if k == 27 or chr(k) == 'q':
                break
            cv.imshow('frame', self.frame)


if __name__ == '__main__':
    app = rtspStream()
    app.update_frame()
